<script language = "Javascript">
listSize = 50;
fetchUsersData = {"getuserlist":"yes",
        "start":0,
        "offset":50}
fetchTotalUserData = {"getTotalUsers":true};
totalEntries = 0;

// Fetch the total number of users
function fetchTotalEntries (data, textStatus) {
totalEntries = data["count"];
}

// Fetch all the users
function fetchUsers (data, textStatus) {
 var contentBody = document.getElementById("topic-wrapper");
 listInit = 0;
 for(i = contentBody.childNodes.length -1 ;i>=0;i--)
	contentBody.removeChild(contentBody.childNodes[i]);

 var newtr = document.createElement('tr');
 heading = {0:"Edit",1:"Username",2:"Email",3:"First Name",4:"Last Name"}
 for(var head in heading)
 {
 	var newtd = document.createElement('td');
	newtd.innerHTML = heading[head];
	newtd.setAttribute('id','topic-header');
	newtr.appendChild(newtd);
 }
 contentBody.appendChild(newtr);

 for(i=0;i<data.length;i++)
 {
	var newtr = document.createElement('tr');
	var row = data[i];

	var newtd = document.createElement('td');
	newtd.innerHTML = "<a href='?action=14&uname="+row['uname']+"'><img src='icons/edit.png' border=0 alt='Edit Entry'></a>";
	newtd.setAttribute('id','topic-edit');
	newtr.appendChild(newtd);

	for(var key in row)
	{
		var newtd = document.createElement('td');
		newtd.innerHTML = row[key];
		newtd.setAttribute('id','topic-body');
		newtr.appendChild(newtd);
	}
	contentBody.appendChild(newtr);
	
	// if listSize has been reached
	if(++listInit == listSize)
			break;
 }
}



// When document is loaded fetch the number of users and fetch the first twenty users
$(document).ready(function(){
	$.post(baseurl+"admin/users.mod.php", fetchTotalUserData,fetchTotalEntries, "json");
	$.post(baseurl+"admin/users.mod.php", fetchUsersData,fetchUsers, "json");
})
function fetchSearch(startsWith)
{
	dataPass = {"searchdata":true,"username":startsWith,"startswith":true};
	$.post(baseurl+"admin/users.mod.php", dataPass,fetchUsers, "json");
}
function searchDB()
{
	regex = document.getElementById("searchbar");
	dataPass = {"searchdata":true,"username":regex.value,"startswith":false};
	$.post(baseurl+"admin/users.mod.php", dataPass,fetchUsers, "json");
}
</script>

<div id="topic-head">List of Users</div>
<div id = "paginlink"></div>
<div id = "searchdiv"><input type='input' name='searchterm' id='searchbar'> <input type='button' value='Search'  id='searchbar' onClick="searchDB()"></div>
<table id="topic-wrapper">
</table>
<script language="JavaScript">
var pagin = document.getElementById("paginlink");
 var pag="";
for(i= 65;i<91;i++)
 {
	var anch = document.createElement('a');
	anch.setAttribute('href',"javascript:fetchSearch('"+String.fromCharCode(i)+"')");
	anch.innerHTML = String.fromCharCode(" "+i+" ");
	pagin.appendChild(anch);
 }
</script>

